<?php

require_once  $_SERVER['DOCUMENT_ROOT']."/../_lib/base/common.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.template.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.hotels.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.destinations.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.exchange_rates.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.photos.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.bookings.php";

// Start Sessions

if($_REQUEST['sid']){
    session_id($_REQUEST['sid']);  
    session_start();
}else{
    session_start();
}

// check for search results and booking timeouts (see: common.php)
if ($location = check_timeouts()) {
    header("Location: $location");
    exit;
}

$template = new template();
$template->title = 'Booking Information';
$template->meta_keywords = $meta_keywords;
$template->meta_description = $meta_description;
$template->section_highlight = 'hotels';
$template->left_col = false;
$template->show_tos = false;
$template->breadcrumbs_html = "<li>&rsaquo;</li><li title=\"Hotels\"><a href=\"/hotels/index.html\" title=\"Hotels\">Hotels</a></li><li>&rsaquo;</li><li title=\"Search Results\"><a href=\"/hotels/hotel_results.php\" title=\"Search Results\">Search Results</a></li>";

$results = $_SESSION["hotels_search_results"]["data"];

switch ($_POST['action']) {
    default:
        if ($_SESSION['booking']['finished']){
            header("Location: index.php");    
        }

        if (count($_POST)) {
            // Delete any old $_SESSION variables relating to previous bookings

            unset($_SESSION['booking']);
            $results = $_SESSION["hotels_search_results"]["data"];
            $hotel_id = intval($_POST["hotel_id"]);
            $hotel = $results["hotels"][$hotel_id];
            //if the user is doing two books in the same time but in different tabs of browser
            if (! is_array($hotel)){
               session_destroy();
               session_start();
               $_SESSION['booking']['finished'] = true;                
               header("Location: booking_error.php"); 
               exit;
            }
            // save booking details
            $room_ids = array();
            // read in room ids
            $i = 1;  
            $total = 0;
            while (isset($_POST["room_id_".$i])) {
                $room_ids[$i] = $_POST["room_id_".$i];
                $total += $hotel["room_sets"][$i]["rooms"][$room_ids[$i]]["Rate"];
                $i++;
            }
            // store booking details
            $_SESSION["booking"]["hotel_id"] = $hotel_id;
            $_SESSION["booking"]["hotel"]    = $hotel;
            $_SESSION["booking"]["room_ids"] = $room_ids;
            $_SESSION["booking"]["total"]    = $total;
//            $booking_details = $_SESSION["booking"];            
            // Self redirect to avoid form resubmission
            header("Location: booking_details.php");
            exit;
        } else {
            $hotel           = $_SESSION["booking"]["hotel"];
            $hotel_id        = $_SESSION["booking"]["hotel_id"];
            $room_ids        = $_SESSION["booking"]["room_ids"];
            $booking_details = $_SESSION["booking"];//["details"];
        }
        
        $template->ld_head();
        
        // Show booking page
        include SITE_ROOT_PATH."/_lib/html/booking_details.php";

        $template->ld_foot();
    break;
    case 'validate';
        $params = map_params_guest($_POST, $_SESSION['hotels_search_params']['data'] );
        $hotel = $_SESSION["booking"]["hotel"];
        $hotel_id = $_SESSION["booking"]["hotel_id"];
        $room_ids = $_SESSION["booking"]["room_ids"];
        $booking_details = $_SESSION["booking"]["details"];
        $bookings = new bookings();
        
        if (!empty($form_errors)) {
            $booking_details = $params;
            $my_message = array();
            $my_message['type'] = 'neg';
            $my_message['message'] = "There was a problem with the form";
            $my_message['list'] = $form_errors;
            $GLOBALS['note'] = $my_message;
            $_SESSION["booking"]["details"] = $params;            
            // Show booking page
            $template->ld_head();
            include SITE_ROOT_PATH."/_lib/html/booking_details.php";
            $template->ld_foot();
        } else {
            // store booking details, including timestamp to track data lifetime
           // print_r($params); die();
            $_SESSION["booking"]["details"] = $params;
            $_SESSION["booking"]["details"]["total"] = $_SESSION["booking"]["total"];
            $_SESSION["booking"]["details"]["total_currency"] = $hotel["currency"];
            $_SESSION["booking"]["details"]["timestamp"] = time();
            // redirect browser to search results page
            // avoid form being resubmitted repeat when refreshing browser
            // save posted values and redirect to next page
                        //print_r($_SESSION); die();    
            // Redirect to a temporary file until payment gateway is active
           header("Location: booking_payment.php");
           // header("Location: booking_temp.php");
            
            exit;
        }
    break;
}

function map_params_guest($params, $ages){
    $roomsCount = count($_SESSION["booking"]["room_ids"]);    
    $guest = array();
    for ($index = 1; $index <= $roomsCount; $index++) {        
        $adults = map_params_guest_by_room_and_type($params, $index, 'adults', $ages); 
        $child = map_params_guest_by_room_and_type($params,  $index, 'children', $ages);       
        $guest[$index]['child'] = $child;
        $guest[$index]['adults'] = $adults;
    }
    $params['guests'] = $guest ;
    echo "params final ";
    return $params;
}

function map_params_guest_by_room_and_type_and_index($params, $room, $typeGuest, $indexGuest, $ages ){
    $title = 'title_room_'.$room.'_'.$typeGuest. '_'.$indexGuest;
    $first_name = 'lead_first_name_room_'.$room.'_'.$typeGuest. '_'.$indexGuest;
    $last_name = 'lead_last_name_room_'.$room.'_'.$typeGuest. '_'.$indexGuest;
    //$age = 'age_room_'.$room.'_'.$typeGuest. '_'.$indexGuest;
    $age = 'room_children_'.$room. '_age_'.$indexGuest ;
    $return = array ('title' => $params[$title], 
                     'first_name' => $params[$first_name], 
                     'last_name' => $params[$last_name],
                     'age' =>($typeGuest == 'adults' ? 0: $ages[$age]));    
    return $return;
}

function map_params_guest_by_room_and_type($params, $room, $typeGuest, $ages){
    $return = array();
    $count = $_SESSION['booking']['hotel']['room_sets'][$room][$typeGuest];
    for ($index = 1; $index <= $count; $index++) {
        $return[$index] = map_params_guest_by_room_and_type_and_index($params, $room, $typeGuest, $index, $ages);
    }
    return $return;
}

